package dhm.com.dhmshop.framework.module.home.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dhm.com.dhmshop.R;
import dhm.com.dhmshop.adapter.home.RecommendShopListAdapter;
import dhm.com.dhmshop.base.Presenter.PressenterImpl;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.base.netWork.LoginContract;
import dhm.com.dhmshop.framework.base.BaseActivity;
import dhm.com.dhmshop.framework.base.MyBaseViewHolder;
import dhm.com.dhmshop.framework.module.home.entity.RecommendShopListEntity;
import dhm.com.dhmshop.framework.module.home.model.RecommendShopListModel;
import dhm.com.dhmshop.framework.module.home.view.RecommendShopListView;
import dhm.com.dhmshop.framework.utils.GlideUtil;
import dhm.com.dhmshop.framework.utils.StringUtil;
import dhm.com.dhmshop.view.mine.myshop.ShopdetailActivity;

public class RecommendShopListActivity extends BaseActivity<RecommendShopListModel> implements LoginContract.IView, View.OnClickListener {

    private RecommendShopListAdapter recommendShopListAdapter;
    private ImageView recommendFan;
    private XRecyclerView recommendShopListRecy;
    private LinearLayout image;
    private ImageView mRecommendFan;
    private TextView jindian_recomshoplist;
    private int offset;
    private PressenterImpl pressenter;
    private List<RecommendShopListEntity.DataBean> alldata;

    @SuppressLint("NewApi")
    @Override
    protected void initView() {
        getWindow().setStatusBarColor(Color.WHITE);
        recommendFan = (ImageView) findViewById(R.id.recommend_fan);
        recommendShopListRecy =  findViewById(R.id.recommend_shop_list);
        image =  findViewById(R.id.image);
        mRecommendFan = (ImageView) findViewById(R.id.recommend_fan);
        mRecommendFan.setOnClickListener(this);
        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        offset=1;
        pressenter=new PressenterImpl();
        pressenter.attachView(this);
        recommendShopListRecy.setLoadingMoreEnabled(true);
        recommendShopListRecy.setPullRefreshEnabled(true);
        recommendShopListRecy.setLayoutManager(manager);

        recommendShopListRecy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                offset=1;
                search();
            }

            @Override
            public void onLoadMore() {
                search();
            }
        });
        search();
    }

    private void search() {
        Map<String,String> map=new HashMap<>();
        map.put("token",Constant.TOKEN);
        map.put("page",offset+"");
        pressenter.sendMessage(RecommendShopListActivity.this,"/appapi/shop/recommend_shop_list",map,RecommendShopListEntity.class);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected int intiLayoutId() {
        return R.layout.activity_recommend_shop_list;
    }

    @Override
    protected RecommendShopListModel initModel() {
        return new RecommendShopListModel();
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.recommend_fan:
                finish();
                break;
            case R.id.jindian_recomshoplist:
                break;
        }
    }

    @Override
    public void requesta(Object data) {
        if (data instanceof RecommendShopListEntity){
            RecommendShopListEntity recommendShopListEntity= (RecommendShopListEntity) data;
            if (recommendShopListEntity.getCode()==1){
                image.setVisibility(View.GONE);
                recommendShopListRecy.setVisibility(View.VISIBLE);
                if (offset==1){
                    alldata = recommendShopListEntity.getData();
                    recommendShopListAdapter = new RecommendShopListAdapter(RecommendShopListActivity.this);
                    recommendShopListRecy.setAdapter(recommendShopListAdapter);
                }else {
                    alldata.addAll(recommendShopListEntity.getData());
                }
                recommendShopListAdapter.setShopList(alldata);
                offset++;
            }else {
                if (offset==1){
                    image.setVisibility(View.VISIBLE);
                    recommendShopListRecy.setVisibility(View.GONE);
                }else {
                    Toast.makeText(this, "暂无更多数据", Toast.LENGTH_SHORT).show();
                }
            }
            recommendShopListRecy.loadMoreComplete();
            recommendShopListRecy.refreshComplete();
        }

    }

    @Override
    public void fail(String error) {

    }

}
